package com.situ.personal.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.User;
import com.situ.personal.service.DepartmentService;
import com.situ.personal.service.UserService;
import com.situ.personal.util.MD5Util;

@WebServlet("/user")
public class UserServlet extends HttpServlet{
	private UserService userService = new UserService();
	private DepartmentService departmentService = new DepartmentService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 处理POST请求乱码问题
		// req.setCharacterEncoding("UTF-8");
		
//		HttpSession session = req.getSession();
//		User user = (User)session.getAttribute("user");
//		if (user == null) {
//			resp.sendRedirect(req.getContextPath() + "/user_login.jsp");
//			return;
//		}
		
		// /Java1912WebPersonal/user?method=findByPage&pageNo=2&pageSize=5
		String method = req.getParameter("method");
		// 如果没有传递method让他的默认值是"findByPage"
		if (method == null || "".equals(method)) {
			method = "findByPage";
		}
		System.out.println("method:" + method);
		switch (method) {
		case "findByPage":
			findByPage(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "getUserInsertPage":
			getUserInsertPage(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		default:
			break;
		}
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean isSuccess = userService.deleteById(Integer.parseInt(id));
		resp.sendRedirect(req.getContextPath() + "/user?method=findByPage");
	}

	// 目的是转法到user_inser.jsp,但是这个页面需要数据List<Department> list
	private void getUserInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		List<Department> list = new ArrayList<Department>();
//		Department department1 = new Department(1, "软件1部");
//		Department department2 = new Department(2, "软件2部");
//		Department department3 = new Department(3, "硬件部");
//		list.add(department1);
//		list.add(department2);
//		list.add(department3);
		
		List<Department> list = departmentService.findAll();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		String departmentId = req.getParameter("departmentId");
		User user = new User(name, Integer.parseInt(age), password, email, Integer.parseInt(departmentId));
		
		boolean isSuccess = userService.insert(user);
		resp.sendRedirect(req.getContextPath() + "/user?method=findByPage");
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = userService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_page_list.jsp").forward(req, resp);
	}
	
	
	
	
	public static void main(String[] args) {
		String str = "123";
		String s =  MD5Util.EncodeUtf8(str);
		System.out.println(s);
	}
	
	
	
	
	
	
}
